<?php
    @include_once("conn.php");
    @include_once("./common.php");

    $user = $_POST["user"];
    $goodsId = $_POST["goodsId"];
    $buyNum = $_POST["buyNum"];

    if(!($user&&$goodsId&&$buyNum)){
        argErr();
    }

    // 首先判断 该用户 是否 买过该商品
    $search = "select * from `shoppingcar` where user='$user' and goodsId = '$goodsId'";
    $result = mysqli_query($conn,$search);

    $item = mysqli_fetch_assoc($result);

    if($item){   //该用户已经买过该商品 买过? 
        $sql = "update `shoppingcar` set buyNum = buyNum + $buyNum where user='$user' and goodsId = '$goodsId'";
    }else{  // 该用户尚未买过该商品   => 就新增
        $sql = "insert into `shoppingcar`(user,goodsId,buyNum) values('$user','$goodsId',$buyNum)";
    }
    
    mysqli_query($conn,$sql);

    $rows = mysqli_affected_rows($conn);
    $msg = array();

    if($rows>0){
        $msg["status"] = true;
        $msg["detail"] = "加入成功";
    }else{
        $msg["status"] = false;
        $msg["detail"] = "加入失败";
        $msg["sql"] = $sql;
    }
 
    echo json_encode($msg);




?>